package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     *  新增菜谱和对应的口味
     * @param dishDTO
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish =new Dish();
        //获取菜谱信息
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);//插入菜谱并返回主键id到dish对象
        //获取菜谱id
        Long dishId = dish.getId();
        //获取口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && !flavors.isEmpty()){
            //给集合的每一个对象设置dishId
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId((dishId));
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜谱分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        long total=page.getTotal();
        List<DishVO> records=page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 根据id查询菜品和口味
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(long id) {
        //根据id查询菜品信息
        Dish dish=dishMapper.getById(id);
        //更具dishId查询口味信息
        List<DishFlavor> flavors =dishFlavorMapper.getById(id);
        //将数据封装到DishVO中
        DishVO dishVO =new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishVO
     */
    @Override
    public void update(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        //更新菜品数据
        dishMapper.update(dish);
        //删除原有的口味数据
        Long dishId =dish.getId();//获取DishID
        dishFlavorMapper.deleteByDishId(dishId);//删除口味信息
        //插入所有的口味数据
        List<DishFlavor> flavors = dishVO.getFlavors();//获取插入数据
        if(flavors != null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否被起售
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                //菜品已起售，抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否被关联
        List<Long> setmealIds =setmealDishMapper.getSetmealIdsByDisIds(ids);
        if(setmealIds!=null && setmealIds.size()>0){
            //菜品被套餐关联
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            //删除菜品
            dishMapper.deleteById(id);
            //删除口味
            dishFlavorMapper.deleteByDishId(id);
        }


    }

    /**
     * 起售和停售菜品
     * @param status
     * @param id
     */

    @Override
    @AutoFill(OperationType.UPDATE)
    public void sartOrStop(Integer status, Long id) {
        Dish dish =Dish.builder()
                        .id(id)
                        .status(status)
                        .build();
        dishMapper.update(dish);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public Dish getByCategoryId(Long categoryId) {
        Dish dish=dishMapper.getByCategoryId(categoryId);
        return dish;
    }
}
